﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Cineman.Resources;
using Cineman.ViewModels;
using Microsoft.Phone.Net.NetworkInformation;
using System.Threading;
using System.IO.IsolatedStorage;
using Newtonsoft.Json;
using Coding4Fun.Toolkit.Controls;

namespace Cineman
{
    public partial class MainPage : PhoneApplicationPage
    {

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data

            BuildLocalizedApplicationBar();
            
        }

        // Load data for the ViewModel Items

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            //Settings.LoadSettings();
            if (!App.ViewModel.IsDataLoaded)
            {

                
                //If Internet is on then load data from web and save it to Storage
                if (DeviceNetworkInformation.IsNetworkAvailable)
                {
                    //Show progressbar overlay
                    Overlay.Visibility = Visibility.Visible;
                    OverlayProgressBar.IsIndeterminate = true;

                    await App.ViewModel.LoadDataAsync();
                    DataContext = App.ViewModel;

                    //Hide progress overlay 
                    Overlay.Visibility = Visibility.Collapsed;
                    OverlayProgressBar.IsIndeterminate = false;

                    //Hide progress overlay 
                    if (!(App.ViewModel.Today == null))
                    {

                        for (int i = 0; i < App.ViewModel.Today.Items.Count; i++)
                        {
                            if (!(App.ViewModel.Today.Items[i].DetailsUrl == null))
                            {
                                App.ViewModel.Today.Items[i].GetDetailsAsync();
                            }
                        }
                        for (int i = 0; i < App.ViewModel.ComingSoon.Items.Count; i++)
                        {
                            if (!(App.ViewModel.ComingSoon.Items[i].DetailsUrl == null))
                            {
                                App.ViewModel.ComingSoon.Items[i].GetDetailsAsync();
                                
                            }
                        }
                    }
                    else
                    {
                        App.ViewModel.LoadData();
                    }
                    //Hide progress overlay 
                    Overlay.Visibility = Visibility.Collapsed;
                    OverlayProgressBar.IsIndeterminate = false;

                }
                //If Internet is off then load data from storage
                else
                {

                    App.ViewModel.LoadData();
                }
            }
        }

        public void throwErrorMessage(string message)
        {
            OverlayError.Visibility = Visibility.Visible;
            ErrorMessage.Text = message;
        }

        private void TodayListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected item is null (no selection) do nothing
            if (TodayList.SelectedItem == null)
                return;

            // Navigate to the new page
            NavigationService.Navigate(new Uri("/MovieDetailsPage.xaml?selectedGroup=Today&selectedItem=" + (TodayList.SelectedItem as MovieData).ID, UriKind.Relative));

            // Reset selected item to null (no selection)
            TodayList.SelectedItem = null;
        }

        private void ComingList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected item is null (no selection) do nothing
            if (ComingList.SelectedItem == null)
                return;

            // Navigate to the new page
            NavigationService.Navigate(new Uri("/MovieDetailsPage.xaml?selectedGroup=ComingSoon&selectedItem=" + (ComingList.SelectedItem as MovieData).ID, UriKind.Relative));

            // Reset selected item to null (no selection)
            ComingList.SelectedItem = null;
        }

        // Sample code for building a localized ApplicationBar
        private void BuildLocalizedApplicationBar()
        {
            // Set the page's ApplicationBar to a new instance of ApplicationBar.
            ApplicationBar = new ApplicationBar();
            ApplicationBar.Mode = ApplicationBarMode.Minimized;

             //Create a new button and set the text value to the localized string from AppResources.
            ApplicationBarIconButton appBarSettings = new ApplicationBarIconButton(new Uri("/Assets/AppBarIcons/feature.settings.png", UriKind.Relative));
            appBarSettings.Text = AppResources.AppBarSettings;
            appBarSettings.Click += appBarSettings_Click;
            ApplicationBar.Buttons.Add(appBarSettings);
            ApplicationBarIconButton appBarAbout = new ApplicationBarIconButton(new Uri("/Assets/AppBarIcons/questionmark.png", UriKind.Relative));
            appBarAbout.Text = AppResources.AppBarAbout;
            appBarAbout.Click += appBarAbout_Click;
            ApplicationBar.Buttons.Add(appBarAbout);

            // Create a new menu item with the localized string from AppResources.
            //ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
            //ApplicationBar.MenuItems.Add(appBarMenuItem);
        }

        void appBarSettings_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/SettingsPage.xaml", UriKind.Relative));
        }

        void appBarAbout_Click(object sender, EventArgs e)
        {
            AboutPrompt aboutPrompt = new AboutPrompt();

            var aboutprompt = new AboutPrompt { Body = new AboutBox() };
            aboutprompt.Show();
        }
    }
}